package com.my.study.design.ruleParallel.suppor;

import com.my.study.design.ruleParallel.AbstractRule;
import com.my.study.design.ruleParallel.entity.AddressEntity;
import com.my.study.design.ruleParallel.entity.User;

import java.util.Optional;

/**
 * @author: yidujun
 * @create: 2021/07/14 10:20
 */
public class AddressRule extends AbstractRule<AddressEntity> {

    private static final String ADDRESS = "湖南";

    @Override
    protected AddressEntity convert(Object obj) {
        if (obj instanceof User) {
            String address = ((User) obj).getAddress();
            AddressEntity addressEntity = new AddressEntity();
            addressEntity.setAddress(address);

            return addressEntity;
        }
        return null;
    }

    @Override
    protected boolean executeRule(AddressEntity addressEntity) {
        Optional.ofNullable(addressEntity).orElseThrow(() -> new RuntimeException("为空"));
        String address = addressEntity.getAddress();
        return ADDRESS.equals(address);
    }
}
